package light.mvc.service.doctor.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.Ttreatment;
import light.mvc.model.register.Tregister;
import light.mvc.model.register.TregisterTreatment;
import light.mvc.page.doctor.RegisterTreatment;
import light.mvc.service.doctor.RegisterTreatmentServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterTreatmentServiceImpl implements RegisterTreatmentServiceI {

	@Autowired
	private BaseDaoI<Tregister> registerDao;

	@Autowired 
	private BaseDaoI<Ttreatment> treatmentDao;
	
	@Autowired 
	private BaseDaoI<TregisterTreatment> registerTreatmentDao;
	
	@Override
	public List<RegisterTreatment> getRegisterTreatment(Integer registerId) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("registerId", registerId);
		
		String hql = "select t from TregisterTreatment t join t.register register " +
					" where register.id = :registerId";
		List<TregisterTreatment> list = registerTreatmentDao.find(hql,params);
		List<RegisterTreatment> registerTreatments = new ArrayList<RegisterTreatment>();
		
		for(TregisterTreatment t : list) {
			RegisterTreatment registerTreatment = new RegisterTreatment();
			
			registerTreatment.setId(t.getId());
			registerTreatment.setTreatmentId(t.getTreatment().getId());
			registerTreatment.setTreatmentItem(t.getTreatment().getTreatmentItem());
			registerTreatment.setChargeTime(t.getChargeTime());
			registerTreatment.setTreatmentTime(t.getTreatmentTime());
			registerTreatment.setResult(t.getResult());

			registerTreatments.add(registerTreatment);
		}
		return registerTreatments;
	}
	
	@Override
	public List<RegisterTreatment> getRegisterTreatmentByPatientId(Integer patientId,Date timeSegment) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("patientId", patientId);
		
		String hql = "select t from TregisterTreatment t join t.register register join " +
					"register.patient patient where patient.id = :patientId ";
		if(timeSegment != null) {
			params.put("timeSegment", timeSegment);
			hql += "and register.timeSegment = :timeSegment";
		}
		
		List<TregisterTreatment> list = registerTreatmentDao.find(hql,params);
		List<RegisterTreatment> registerTreatments = new ArrayList<RegisterTreatment>();
		
		for(TregisterTreatment t : list) {
			RegisterTreatment registerTreatment = new RegisterTreatment();
			
			registerTreatment.setRegisterId(t.getRegister().getId());
			registerTreatment.setTimeSegment(t.getRegister().getTimeSegment());
			registerTreatment.setSeq(t.getRegister().getSeq());
			registerTreatment.setDepartmentName(t.getRegister().getDoctor().getDepartment().getName());
			registerTreatment.setDoctorName(t.getRegister().getDoctor().getName());
			
			registerTreatment.setId(t.getId());
			registerTreatment.setTreatmentId(t.getTreatment().getId());
			registerTreatment.setTreatmentItem(t.getTreatment().getTreatmentItem());
			registerTreatment.setChargeTime(t.getChargeTime());
			registerTreatment.setTreatmentTime(t.getTreatmentTime());
			registerTreatment.setResult(t.getResult());

			registerTreatments.add(registerTreatment);
		}
		return registerTreatments;
	}
	
	@Override
	public void addTreatment(RegisterTreatment registerTreatment) {
		Ttreatment treatment = treatmentDao.get(Ttreatment.class, registerTreatment.getTreatmentId());
		Tregister register = registerDao.get(Tregister.class, registerTreatment.getRegisterId());
		
		TregisterTreatment t = new TregisterTreatment();
		
		t.setTreatment(treatment);
		t.setRegister(register);
		t.setTreatmentTime(new Date());
		
		registerTreatmentDao.save(t);
	}
	
	@Override
	public void charge(Integer registerTreatmentId) {
		TregisterTreatment t = registerTreatmentDao.get(TregisterTreatment.class, registerTreatmentId);
		
		t.setChargeTime(new Date());
		
		registerTreatmentDao.update(t);
	}
	
	@Override
	public void edit(Integer registerTreatmentId,String result) {
		TregisterTreatment t = registerTreatmentDao.get(TregisterTreatment.class, registerTreatmentId);
		
		t.setResult(result);
		
		registerTreatmentDao.update(t);
	}
}
